package com.leyou.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.dto.SkuDTO;
import com.leyou.dto.SpecParamDTO;
import com.leyou.dto.SpuDTO;
import com.leyou.dto.SpuDetailDTO;
import com.leyou.service.GoodsService;
import com.leyou.service.SkuService;
import com.leyou.service.SpuDetailService;
import com.leyou.service.SpuService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    //构造器注入
    @Autowired
    private final SpuService spuService;
    @Autowired
    private final SpuDetailService spuDetailService;
    @Autowired
    private final SkuService skuService;
    @Autowired
    private  GoodsService goodsService;



    public GoodsController(SpuService SpuService, SpuDetailService spuDetailService, SkuService skuService) {
        this.spuService = SpuService;
        this.spuDetailService = spuDetailService;
        this.skuService = skuService;
    }

    /*
    * TODO
    *    商品分页查询
    * */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(@RequestParam(value = "id",required = false) Long id,//商品spu的ID
                                                          @RequestParam(value = "brandId",required = false)Long brandId,//品牌id
                                                          @RequestParam(value = "categoryId",required = false)Long categoryId,//商品分类id
                                                          @RequestParam(value = "page",defaultValue = "1")Integer page,//当前页码
                                                          @RequestParam(value = "rows",defaultValue = "5")Integer rows,//每页大小
                                                          @RequestParam(value = "saleable",required = false)Boolean saleable){//是否上架

        return ResponseEntity.ok(this.spuService.querySpuByPage(id,brandId,categoryId,page,rows,saleable));
    }


    /*
     * todo 根据spuId查询商品Spu
     *  */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO>querySpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.spuService.querySpuById(id));
    }

    /*
    * TODO
    *  根据spuId查询对应的sku的集合
    * */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>listSkuBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(this.skuService.listSkuBySpuId(id));
    }

    /*
    * todo
    *  根据sku的id集合查询SKU集合
    * */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>>listSkuBySpuIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(this.skuService.listSkuBySpuIds(ids));
    }



    /*
    * todo
    *   根据spuId查询SpuDetail(商品详情)
    * */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO>querySpuDetailById(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.spuDetailService.querySpuDetailById(id));
    }


    /*
    * todo
    *   根据id查询商品spu及sku、spuDetail等
    * */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO>queryGoodsDetailById(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.goodsService.queryGoodsDetailById(id));
    }

    /*
    * todo
    *  查询商品规格参数键值对
    * */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>>querySpecsValues(@RequestParam("id") Long spuId,
                                                              @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(this.goodsService.querySpecsValues(spuId,searching));
    }


    /*
    * TODO
    *  商品新增
    * */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        this.spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CONTINUE).build();
    }

    /*
    * todo 商品上下架
    *  */
    @PutMapping("/saleable")
    public ResponseEntity<Void>updateSpuSaleable(@RequestParam("id")Long spuId,
                                                 @RequestParam("saleable") Boolean saleable){
        this.spuService.updateSpuSaleable(spuId,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /*
    * todo 商品修改 可能包含spu，sku以及spuDetail
    *  */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestParam SpuDTO spuDTO){
        this.spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

}
